import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_custom_bottom_tab_bar/eachtab.dart';
import 'package:flutter_custom_bottom_tab_bar/tabbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/set/DrawMoney.dart';
import 'package:panda/set/ShouRuChild.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/SignUtil.dart';

import 'BalanceRecord.dart';

class MyShouRuPage extends StatefulWidget {
  MyShouRuPage({Key key}) : super(key: key);

  @override
  _MyShouRuPageState createState() => _MyShouRuPageState();
}

class _MyShouRuPageState extends State<MyShouRuPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  var titles = ['待结算', '已结算'];
  String balance = "";
  String today = "";
  String month = "";
  String allbalance = "";
  String token;
  @override
  void initState() {
    super.initState();
    _tabController =
        new TabController(vsync: this, initialIndex: 0, length: titles.length);
    getString(TOKEN).then((value) {
      token = value;
      loadData();
    });
  }

  loadData() async {
    Map map = new Map();
    map['token'] = token;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/User/getMyProfit",
        data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }

    if (mounted)
      setState(() {
        balance = userMap['data']['balance'] == null
            ? "0"
            : userMap['data']['balance'].toString();
        today = userMap['data']['today'].toString();
        month = userMap['data']['month'].toString();
        allbalance = userMap['data']['allbalance'].toString();
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '我的收入',
          style: TextStyle(
              color: black,
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
        ),
        elevation: 0.2,
        backgroundColor: white,
        iconTheme: new IconThemeData(color: black_99),
        brightness: Brightness.light,
        actions: <Widget>[
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Navigator.of(context)
                  .push(new MaterialPageRoute(builder: (context) {
                return new BalanceRecordPage();
              }));
            },
            child: Container(
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              child: Text(
                '余额明细',
                style: TextStyle(
                    color: mainColor,
                    fontSize: ScreenUtil.getInstance().setSp(28)),
              ),
            ),
          )
        ],
      ),
      body: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.all(10),
            padding: EdgeInsets.all(10),
            width: double.infinity,
            height: MediaQuery.of(context).size.width * 2 / 5,
            decoration: BoxDecoration(
                color: mainColor, borderRadius: BorderRadius.circular(10)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                   Expanded(child: Center()),
                Row(
                  children: <Widget>[
                    Expanded(
                        child: Column(
                      children: <Widget>[
                        Text(
                          '$allbalance',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(35)),
                        ),
                        Text(
                          '总收入(元)',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(22)),
                        )
                      ],
                    )),
                    Container(
                      width: 0.5,
                      color: white,
                      height: 30,
                    ),
                    Expanded(
                        child: Column(
                      children: <Widget>[
                        Text(
                          '$month',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(35)),
                        ),
                        Text(
                          '本月收入(元)',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(22)),
                        )
                      ],
                    )),
                    Container(
                      width: 0.5,
                      color: white,
                      height: 30,
                    ),
                    Expanded(
                        child: Column(
                      children: <Widget>[
                        Text(
                          '$today',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(35)),
                        ),
                        Text(
                          '今日收入(元)',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(22)),
                        )
                      ],
                    )),
                  ],
                ),
                Container(
                  height: 8,
                ),
                Divider(
                  color: white,
                ),
                Expanded(child: Center()),
                     Row(
                  children: <Widget>[
                    Expanded(
                        child: Column(
                      children: <Widget>[
                      ],
                    )),
                    Expanded(
                        child: Column(
                      children: <Widget>[
                        Text(
                          '$balance',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(35)),
                        ),
                        Text(
                          '可提现金额(元)',
                          style: TextStyle(
                              color: white,
                              fontSize: ScreenUtil.getInstance().setSp(22)),
                        )
                      ],
                    )),
                    Expanded(
                        child: Column(
                      children: <Widget>[
                          GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (context) {
                          return DrawMoneyPage(total: balance);
                        }));
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 3, 20, 3),
                        decoration: BoxDecoration(
                            color: white,
                            borderRadius: BorderRadius.circular(30)),
                        child: Text(
                          '提现',
                          style: TextStyle(
                              color: mainColor,
                              fontSize: ScreenUtil.getInstance().setSp(24)),
                        ),
                      ),
                    )
                
                      ],
                    )),
                  ],
                ),
               Container(
                  height: 5,
                ),
                 Expanded(child: Center()),
              ],
            ),
          ),
          Expanded(
              child: Container(
            child: Column(
              children: <Widget>[
                TabContainer(
                    width: MediaQuery.of(context).size.width,
                    alignment: Alignment.center,
                    color: Colors.white,
                    child: Stack(overflow: Overflow.clip, children: <Widget>[
                      Container(
                          child: new TabBar(
                        isScrollable: true,
                        controller: _tabController,
                        indicatorColor: mainColor,
                        labelColor: mainColor,
                        labelPadding: EdgeInsets.all(0),
                        unselectedLabelColor: Colors.black,
                        tabs: <Widget>[
                          EachTab(
                            text: titles[0],
                            textStyle: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(NORMAL_FONT_SIZE - 2)),
                          ),
                          EachTab(
                            text: titles[1],
                            textStyle: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(NORMAL_FONT_SIZE - 2)),
                          ),
                        ],
                      ))
                    ])),
                Expanded(
                    child: TabBarView(
                        controller: _tabController,
                        children: <Widget>[
                      Center(
                          child: new ShouRuChildPage(
                        type: 1,
                      )),
                      Center(
                          child: ShouRuChildPage(
                        type: 2,
                      )),
                    ]))
              ],
            ),
          ))
        ],
      ),
    );
  }
}
